from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from PIL import Image


img_path = "image/1_3_149.jpg"
img = Image.open(img_path)
print(img)

writer = SummaryWriter("logs")


#ToTensor的使用
trans_tensor = transforms.ToTensor()
img_tensor = trans_tensor(img)
print(img_tensor[0][0][0])
writer.add_image("img_ToTensor",img_tensor)


#Normalize的使用
norm = transforms.Normalize([6,3,2],[9,3,5])
#(input[channel]-mean[channel])/std[channel]
img_norm = norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("Normalize",img_norm,2)

#Resize的使用
print(img.size)
trans_resize = transforms.Resize((512,512))
img_resize = trans_resize(img)
print(img_resize)
print(img_resize.size)
img_resize_tensor = trans_tensor(img_resize)
#img PIL-->img_resize PIL-->img_resize_tensor tensor
print(img_resize_tensor)
writer.add_image("Resize_img",img_resize_tensor,0)

#上面得过程为将PIL类型得图片img进行了Resize,之后该图片仍然为PIL格式，然后将该Resize之后得图片ToTensor为tensor，最后使用tensorboard进行展示
#上面得过程可以使用transforms的Compose进行集合在一起实现
#Compose--Resize的第二种使用(不改变高和宽的比例，只使用一个整数来改变高和宽的大小关系）
trans_resize_2 = transforms.Resize(512)
#PIL-->PIL-->tensor
trans_compose = transforms.Compose([trans_resize_2,trans_tensor])#compose里面需要的是一个列表，切实transforms类型的
img_resize_2 = trans_compose(img)
#img_tensor_2 = trans_tensor(img_resize_2)
writer.add_image("Resize_img",img_resize_2,1)
# trans_compose = transforms.Compose([
#     transforms.Resize(img),
#     transforms.ToTensor(img),
#
# ])
#随机裁剪RandomCrop
trans_random = transforms.RandomCrop(512)
#trans_random = transforms.RandomCrop(512,3000)
trans_compose_2 = transforms.Compose([trans_random,trans_tensor])
for i in range(10):
    img_crop = trans_compose_2(img)
    writer.add_image("RandomCrop",img_crop,i)

writer.close()
